﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace SolutionFramework.Tests
{
    public partial class Progress : Form
    {
        private int maximum;
        private int value;
        public string ProgressStatusSuffix;

        public Progress()
        {
            InitializeComponent();
        }

        public string Title
        {
            get
            {
                return this.Text;
            }

            set
            {
                this.Text = value;
            }
        }

        public void WriteStatus(string status)
        {
            if (LogListBox.InvokeRequired)
            {
                LogListBox.Invoke(new ThreadStart(() =>
                {
                    var index = LogListBox.Items.Add(status);
                    LogListBox.SelectedIndex = index;
                }));
            }
            else
            {
                var index = LogListBox.Items.Add(status);
                LogListBox.SelectedIndex = index;
            }

            Application.DoEvents();
        }

        public int Maximum
        {
            set
            {
                if (ProgressBar.InvokeRequired)
                {
                    ProgressBar.Invoke(new ThreadStart(() =>
                    {
                        maximum = value;
                        ProgressBar.Maximum = maximum;
                    }));
                }
                else
                {
                    maximum = value;
                    ProgressBar.Maximum = maximum;
                }
            }
        }

        public int Value
        {
            set 
            {
                if (ProgressBar.InvokeRequired)
                {
                    ProgressBar.Invoke(new ThreadStart(() =>
                    {
                        this.value = value;
                        ProgressBar.Value = value;

                        ProgressStatusLabel.Text = string.Format("{0} of {1} {2}.", value, maximum, this.ProgressStatusSuffix);
                    }));
                }
                else
                {
                    this.value = value;
                    ProgressBar.Value = value;

                    ProgressStatusLabel.Text = string.Format("{0} of {1} {2}.", value, maximum, this.ProgressStatusSuffix);
                }

                Application.DoEvents();
            }
        }
    }
}
